
from django.views import View
from django.db import transaction

from User.models import OrderVip
from Venue.models import Vip
from sportsground.utils import Req
from sportsground.utils.Res import Res
from sportsground.utils.views import LoginRequiredUser


class VipView(LoginRequiredUser, View):
    """ 会员卡订单 """

    @Req.getJson
    def post(self, request):
        """ 添加 """
        vipId = request.json_dict.get('vipId')
        message = request.json_dict.get('message')

        user = request.user.user

        with transaction.atomic():
            vip = Vip.objects.select_for_update().filter(id=vipId).first()
            if not vip:
                return Res.Json(Res.ParamErr)

            order = OrderVip.create(user, vip=vip, message=message)
            return Res.Json(Res.OK, result=order.Info())
